#include <iostream>
using namespace std;

class Complex {
public:
	//L#R c1+c2 => c1.operator+(c2); 左调右参
	//因为返回值为右值, 所以需要加const
	//如果c1为常对象，只能调用常函数, 所以函数也要声明为常函数
	Complex (int r, int i):m_r(r),m_i(i) {}
	const Complex operator+ (const Complex& c) const {
		Complex res(m_r+c.m_r, m_i+c.m_i);
		return res;
	}	
	void print (void) {
		cout << m_r << '+' << m_i << 'i' << endl;
	}
private:
	int m_r; //实部
	int m_i; //虚部
	//friend 修饰的函数可以访问类中的私有成员 
	friend const Complex operator- (const Complex& l, const Complex& r);
	friend const Complex operator* (const Complex& l, const Complex& r);
};

//::operator-(L,R) 
const Complex operator- (const Complex& l, const Complex& r)
{
	Complex res (l.m_r-r.m_r, l.m_i-r.m_i);
	return res;
}
//::operator*(L,R)
const Complex operator* (const Complex& l, const Complex& r)
{
	Complex res ((l.m_r*r.m_r - l.m_i*r.m_i), (l.m_i*r.m_r + l.m_r*r.m_i));
	return res;
}

int main(void)
{
	Complex c1 (3,4);
	Complex c2 (1,2);
	c1.print ();
	c2.print ();
	//error 需要重载'+'运算符
	Complex c3 = c1 * c2;
	c3.print ();

	return 0;
}
